package com.share.wechat.core.controller;

import com.share.wechat.core.entity.Detail;
import com.share.wechat.core.service.DetailService;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.validation.constraints.NotNull;
import java.util.List;

@RestController
@RequestMapping("/core/detail/")
public class DetailController {
    private Logger logger = Logger.getLogger(this.getClass());
    @Value("${image.file.url}")
    private String PHOTO_URL;
    @Resource
    private DetailService detailService;

    /**
     * 根据前段传过来用户的大类信息
     * 过度出用户选择的信息
     *
     * @param dataSort 大类
     * @param page     页数
     * @param size     条数
     * @return List<Detail>
     */
    @Deprecated
    @GetMapping("love")
    public List<Detail> showLoveDetail(@RequestParam(value = "dataSort", required = false, defaultValue = "10") int dataSort, @RequestParam(value = "page", required = false, defaultValue = "0") int page, @RequestParam(value = "size", required = false, defaultValue = "20") int size) {

        List<Detail> details = detailService.getDetailLoveBySort(dataSort, page, size);
        if (logger.isInfoEnabled()) {
            logger.info("查询到的数据为" + details);
            logger.info("该用户偏爱的方向为" + details);
        }
        return details;
    }

    /**
     * 显示某一条详细信息的时候调用的方法
     *
     * @param id id
     * @return 单个详细信息
     */
    @GetMapping("showDetailById")
    public Detail showDetailById(String id) {
        if (StringUtils.isEmpty(id)) {
            logger.info("----id 为空 不能查询到有效数据,id为:" + id);
            return null;
        }
        Detail detail = detailService.showById(id);
        logger.info("查询到的数据为" + detail);
        return detail;
    }


    /**
     * 根据后台自动计算出用户
     * 喜欢的那部分数据
     * 对应前台高分推荐部分
     *
     * @param userInfo userInfo
     * @param page     page
     * @param size     size
     * @return 集合
     */
    @GetMapping("showDetailUserInfo")
    public List<Detail> showLoveByUserInfo(@NotNull String userInfo, @RequestParam(value = "page", required = false, defaultValue = "0") int page, @RequestParam(value = "size", required = false, defaultValue = "3") int size) {
        logger.debug("userInfo信息为" + userInfo);
        List<Detail> details = detailService.getDetailByUserInfo(userInfo, page, size);
        logger.debug("查询到的数据为" + details);
        for (Detail detail : details) {
            detail.setImageUrl(PHOTO_URL + detail.getImageUrl());
        }
        return details;
    }


    /**
     * 根据用户推荐出没有购买或者看过的数据
     * 对应前台的推荐广场部分数据
     *
     * @param page 分页
     * @param size 条数
     * @return 查询出的集合
     */
    @GetMapping("getRecommendDetail")
    public List<Detail> getRecommendDetail(@RequestParam(value = "page", required = false, defaultValue = "0") int page, @RequestParam(value = "size", required = false, defaultValue = "3") int size) {
        List<Detail> result = detailService.getRecommendDetail(page, size);
        logger.debug("查询到的数据条数为" + result.size());
        for (Detail detail : result) {
            detail.setImageUrl(PHOTO_URL + detail.getImageUrl());
        }
        return result;
    }


}
